package com.wswy.wyjk.ui.practice

import android.app.Dialog
import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.graphics.Color
import android.os.Bundle
import android.support.design.widget.BottomSheetBehavior
import android.support.design.widget.BottomSheetDialog
import android.support.design.widget.BottomSheetDialogFragment
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.raft.framework.utils.UICompat
import com.wswy.wyjk.R

class RecordDialog : BottomSheetDialogFragment() {

    companion object {
        fun newInstance():RecordDialog{
            return RecordDialog()
        }
    }

    private lateinit var practiceViewModel: PracticeViewModel

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return super.onCreateDialog(savedInstanceState).also {

           val view = LayoutInflater.from(context).inflate(R.layout.layout_record_dialog,null)

            it.setContentView(view)

            (view.parent as? ViewGroup)?.setBackgroundColor(Color.TRANSPARENT)

        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        fixDialog()

        practiceViewModel = ViewModelProviders.of(requireActivity()).get(PracticeViewModel::class.java)


        val recyclerView : RecyclerView = dialog?.findViewById(R.id.recyclerView)?:return


        dialog?.findViewById<TextView>(R.id.tv_curr_position)?.text = "${practiceViewModel.uiData.currentPosition+1}"

        dialog?.findViewById<TextView>(R.id.tv_q_count)?.text = "/${practiceViewModel.uiData.count}"

        dialog?.findViewById<TextView>(R.id.tv_right_count)?.text  = "${practiceViewModel.uiData.rightCount}"

        dialog?.findViewById<TextView>(R.id.tv_wrong_count)?.text  = "${practiceViewModel.uiData.wrongCount}"


        recyclerView.layoutManager = GridLayoutManager(requireContext(),6)

        val adapter = RecordAdapter()


        adapter.setOnItemClickListener { _, position, t ->

            dismiss()

            practiceViewModel.onRecordDismiss(position)
        }


        recyclerView.adapter = adapter


        practiceViewModel.getPracticeLiveData().observe(this, Observer {
            it?.run {

                adapter.setNewData(it)

                adapter.notifyDataSetChanged()

                //跳到指定位置
                if (practiceViewModel.uiData.currentPosition > 30){
                    recyclerView.scrollToPosition(practiceViewModel.uiData.currentPosition)
                }

            }

        })



    }

    private fun fixDialog(){
        // https://stackoverflow.com/questions/37555403/android-bottomsheetdialogfragment-does-not-expand-completely
        dialog.setOnShowListener {

            (it as? BottomSheetDialog?)?.findViewById<View>(R.id.design_bottom_sheet)?.let {

                BottomSheetBehavior.from(it).peekHeight = UICompat.dp2px(480)

                it.parent?.parent?.requestLayout()
            }

        }
    }

}